package com.atguigu.msm.service.impl;

import cn.hutool.core.lang.hash.Hash32;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.msm.service.MsmService;
import com.atguigu.utils.RandomUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.hash.HashMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Summerday
 */

@Service
public class MsmServiceImpl implements MsmService {


    private static final Logger log = LoggerFactory.getLogger(MsmServiceImpl.class);

    @Resource
    private IAcsClient acsClient;

    @Value("${aliyun.msm.file.regionId}")
    private String regionId;

    @Value("${aliyun.msm.file.signName}")
    private String signName;

    @Value("${aliyun.msm.file.templateCode}")
    private String templateCode;

    @Resource
    private CommonRequest request;

    @Override
    public boolean sendMsm(Map<String, Object> param, String phone) {

        if(StringUtils.isEmpty(phone)) return false;

        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", phone);//电话
        request.putQueryParameter("SignName",signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));//验证码
        try {
            CommonResponse response = acsClient.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            log.error(e.getErrMsg());
            return false;
        }
    }
}

